package com.dhcc.bpm.modules.safe.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dhcc.bpm.common.annotation.Log;
import com.dhcc.bpm.common.enums.BusinessType;
import com.dhcc.bpm.common.enums.ConfigType;
import com.dhcc.bpm.common.redisMessage.RedisMessageSender;
import com.dhcc.bpm.modules.gateway.service.IGatewayService;
import com.dhcc.bpm.modules.gateway.vo.GatewayVO;
import com.dhcc.bpm.modules.orga.service.IAppService;
import com.dhcc.bpm.modules.orga.service.IOrgaService;
import com.dhcc.bpm.modules.orga.vo.AppVO;
import com.dhcc.bpm.modules.orga.vo.OrgaVO;
import com.dhcc.bpm.modules.safe.entity.GatewayControl;
import com.dhcc.bpm.modules.safe.service.*;
import com.dhcc.bpm.modules.safe.vo.GatewayControlVO;
import com.dhcc.bpm.modules.safe.vo.IpGatewayVO;
import com.dhcc.bpm.modules.system.service.ISerialNumberService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.Map;
/**
 * @ClassName GatewayControlController
 * @Description 网关访问控制表 控制器
 * @Author daina
 * @Date 2020/2/21 14:16
 * @Version V1.0
 * @Copyright: 2020 东华软件 . All rights reserved.
 */
@RestController
@AllArgsConstructor
@RequestMapping("bpm-safe/gatewaycontrol")
@Api(value = "网关访问控制表", tags = "网关访问控制表接口")
public class GatewayControlController extends BladeController {
	private static final Logger log = LoggerFactory.getLogger(GatewayControlController.class);

	private IGatewayControlService gatewayControlService;

	private ISerialNumberService serialNumberService;
	@Autowired
	private RedisMessageSender redisMessageSender;
	@Autowired
	private IGatewayService gatewayService;
	@Autowired
	private IOrgaService orgaService;
	@Autowired
	private IAppService appService;
	@Autowired
	private IIpGatewayService ipGatewayService;
	@Autowired
	private IGateControlService gateControlService;
	@Autowired
	private IOrgaGatewayService orgaGatewayService;
	@Autowired
	private IAppGatewayService appGatewayService;

	/**
	 * @Description 详情
	 * @Author daina
	 * @Date 9:57 2020/2/22
	 * @param gatewayControl
	 * @return org.springblade.core.tool.api.R<com.dhcc.bpm.modules.safe.entity.GatewayControl>
	 **/
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入gatewayControl")
	@ApiLog("详情")
	@Log(title = "查询",businessType = BusinessType.SELECT)
	public R<GatewayControl> detail(GatewayControl gatewayControl) {
		GatewayControl detail = gatewayControlService.getOne(Condition.getQueryWrapper(gatewayControl));
		return R.data(detail);
	}
	/**
	 * @Description 修改 控制状态
	 * @Author daina
	 * @Date 9:57 2020/2/22
	 * @param gatewayControl
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/changeStatus")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入gatewayControl")
	@ApiLog("修改控制状态")
	@Log(title = "修改控制状态", businessType = BusinessType.UPDATE)
	public R changeStatus(@Valid @RequestBody GatewayControl gatewayControl) {
		gatewayControl.setRefreshFlag("1");
		return R.status(gatewayControlService.updateById(gatewayControl));
	}
	/**
	 * @Description 分页 网关访问控制表
	 * @Author daina
	 * @Date 9:58 2020/2/22
	 * @param gatewayControl
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.safe.entity.GatewayControl>>
	 **/
	@GetMapping("/list")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "controlScope", value = "控制范围", paramType = "query", dataType = "string"),
			@ApiImplicitParam(name = "controlName", value = "控制名称", paramType = "query", dataType = "string"),
			@ApiImplicitParam(name = "controlCode", value = "控制编号", paramType = "query", dataType = "string"),
			@ApiImplicitParam(name = "strategyType", value = "策略类型", paramType = "query", dataType = "string"),
			@ApiImplicitParam(name = "controlStatus", value = "控制状态", paramType = "query", dataType = "string")
	})
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入gatewayControl")
	@ApiLog("分页 网关访问控制表")
	@Log(title = "分页 网关访问控制表",businessType = BusinessType.SELECT)
	public R<IPage<GatewayControl>> list(@ApiIgnore @RequestParam Map<String, Object> gatewayControl, Query query) {
		IPage<GatewayControl> pages = gatewayControlService.page(Condition.getPage(query), Condition.getQueryWrapper(gatewayControl,GatewayControl.class));
		return R.data(pages);
	}

	/**
	 * @Description 自定义分页 网关访问控制表
	 * @Author daina
	 * @Date 9:58 2020/2/22
	 * @param gatewayControl
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.safe.vo.GatewayControlVO>>
	 **/
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入gatewayControl")
	@ApiLog("自定义分页 网关访问控制表")
	@Log(title = "自定义分页 网关访问控制表",businessType = BusinessType.SELECT)
	public R<IPage<GatewayControlVO>> page(GatewayControlVO gatewayControl, Query query) {
		IPage<GatewayControlVO> pages = gatewayControlService.selectGatewayControlPage(Condition.getPage(query), gatewayControl);
		return R.data(pages);
	}

	/**
	 * @Description 新增 网关访问控制表
	 * @Author daina
	 * @Date 9:58 2020/2/22
	 * @param gatewayControl
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入gatewayControl")
	@ApiLog("新增")
	@Log(title = "新增",businessType = BusinessType.INSERT)
	public R save(@Valid @RequestBody GatewayControl gatewayControl) {
		return R.status(gatewayControlService.save(gatewayControl));
	}

	/**
	 * @Description 修改 网关访问控制表
	 * @Author daina
	 * @Date 9:58 2020/2/22
	 * @param gatewayControl
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入gatewayControl")
	@ApiLog("修改")
	@Log(title = "修改",businessType = BusinessType.UPDATE)
	public R update(@Valid @RequestBody GatewayControl gatewayControl) {
		return R.status(gatewayControlService.updateById(gatewayControl));
	}

	/**
	 * @Description 新增或修改 网关访问控制表
	 * @Author daina
	 * @Date 9:59 2020/2/22
	 * @param gatewayControl
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入gatewayControl")
	@ApiLog("新增网关黑白名单")
	@Log(title = "新增网关黑白名单", businessType = BusinessType.INSERT)
	public R submit(@Valid @RequestBody GatewayControl gatewayControl) {
		if(gatewayControl.getId() == null){
			// 设置控制编号
			String code = serialNumberService.generateSerialNumberByModelCode("opc_gateway_control");
			gatewayControl.setControlCode(code);

		}
		gatewayControl.setRefreshFlag("1");
		gatewayControl.setControlName(gatewayControl.getControlName().trim());
		try {
			return R.status(
					gatewayControlService.saveOrUpdate(gatewayControl));
		} catch (Exception e) {
			if (e.getMessage().contains("for key 'name_scope_idx'") || e.getMessage().contains("ORA-00001")) {
				return R.fail(500, "该控制名称已存在");
			}
			return R.fail(500, "数据库异常");
		} finally {

		}


	}

	
	/**
	 * @Description 删除 网关访问控制表
	 * @Author daina
	 * @Date 9:59 2020/2/22
	 * @param ids
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	@ApiLog("删除")
	@Log(title = "删除",businessType = BusinessType.DELETE)
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(gatewayControlService.deleteLogic(Func.toLongList(ids)));
	}

	/**
	 * @Description 刷新API黑白名单
	 * @Author daina
	 * @Date 9:59 2020/2/22
	 * @param env
	 * @param controlScope
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/refreshGate")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "刷新网关黑白名单")
	@ApiLog("刷新网关黑白名单")
	@Log(title = "刷新网关黑白名单", businessType = BusinessType.REFRESH)
	public R refreshAppSecret(@ApiParam(value = "主键集合", required = true) @RequestParam String env,@RequestParam String controlScope) {
		try {
			String message = redisMessageSender.format(ConfigType.GATEWAYWHITEBLACK.name(), "刷新网关黑白名单");
			redisMessageSender.sendMessageToGateway("", message);
			GatewayControl gatewayControl = new GatewayControl();
			// 刷新标志 2：已刷新
			gatewayControl.setRefreshFlag("2");
			gatewayControl.setEnv(env);
			gatewayControl.setControlScope(controlScope);

			int result = gatewayControlService.updateRefreshFlag(gatewayControl);
			if(result>=0){
				return R.status(true);
			}
			return R.status(false);
		}catch (Exception e){
			log.error("刷新网关黑白名单失败:{}", e);
			return R.status(false);
		}
	}

	/**
	 * @Description 自定义分页 网关表
	 * @Author daina
	 * @Date 9:59 2020/2/22
	 * @param gateway
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.gateway.vo.GatewayVO>>
	 **/
	@GetMapping("/selectGateControllerGateList")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入gateway")
	@ApiLog("详情")
	@Log(title = "查询",businessType = BusinessType.SELECT)
	public R<IPage<GatewayVO>> selectControllerGatePage(@ApiIgnore GatewayVO gateway, Query query) {
		IPage<GatewayVO> pages = gatewayService.selectGatewayPage(Condition.getPage(query), gateway);
		return R.data(pages);
	}

	/**
	 * @Description 自定义分页 机构信息表
	 * @Author daina
	 * @Date 9:59 2020/2/22
	 * @param orga
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.orga.vo.OrgaVO>>
	 **/
	@GetMapping("/selectGateControllerOrgaList")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入orga")
	@ApiLog("自定义分页 机构信息表")
	@Log(title = "自定义分页 机构信息表",businessType = BusinessType.SELECT)
	public R<IPage<OrgaVO>> selectControllerOrgaPage(@ApiIgnore OrgaVO orga, Query query) {
		IPage<OrgaVO> pages = orgaService.selectOrgaPage(Condition.getPage(query), orga);
		return R.data(pages);
	}

	/**
	 * @Description 自定义分页 应用/渠道信息
	 * @Author daina
	 * @Date 9:59 2020/2/22
	 * @param app
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.orga.vo.AppVO>>
	 **/
	@GetMapping("/selectGateControllerAppList")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入app")
	@ApiLog("自定义分页 应用/渠道信息")
	@Log(title = "自定义分页 应用/渠道信息",businessType = BusinessType.SELECT)
	public R<IPage<AppVO>> selectGateControllerAppList(@ApiIgnore AppVO app, Query query) {
		IPage<AppVO> pages = appService.selectAppPage(Condition.getPage(query), app);
		return R.data(pages);
	}

	/**
	 * @Description 自定义分页 网关访问控制IP关联表
	 * @Author daina
	 * @Date 9:59 2020/2/22
	 * @param ipGateway
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.safe.vo.IpGatewayVO>>
	 **/
	@GetMapping("/selectGateControllerIpList")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入ipGateway")
	@ApiLog("自定义分页")
	@Log(title = "自定义分页",businessType = BusinessType.SELECT)
	public R<IPage<IpGatewayVO>> selectGateControllerIpList(@ApiIgnore IpGatewayVO ipGateway, Query query) {
		IPage<IpGatewayVO> pages = ipGatewayService.selectIpGatewayPage(Condition.getPage(query), ipGateway);
		return R.data(pages);
	}

	/**
	 * @Description 查询未分配的网关列表
	 * @Author daina
	 * @Date 10:00 2020/2/22
	 * @param gateway
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.gateway.vo.GatewayVO>>
	 **/
	@GetMapping("/selectUnControllerGatewayList")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入gateway")
	@ApiLog("查询未分配的网关列表")
	@Log(title = "查询未分配的网关列表",businessType = BusinessType.SELECT)
	public R<IPage<GatewayVO>> selectUnControllerGatewayList(@ApiIgnore GatewayVO gateway, Query query) {
		IPage<GatewayVO> pages = gatewayService.selectUnControllerGatewayList(Condition.getPage(query), gateway);
		return R.data(pages);
	}

	/**
	 * @Description 批量选择网关
	 * @Author daina
	 * @Date 10:00 2020/2/22
	 * @param opcControlId
	 * @param ids
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/AuthControllerGateAll")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入gateway")
	@ApiLog("批量添加网关信息")
	@Log(title = "批量添加网关信息", businessType = BusinessType.INSERT)
	public R asuthControllerGateAll(@ApiParam(name ="opcControlId", value = "控制id") @RequestParam Long  opcControlId,@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return  R.status(gateControlService.batchControllerGate(opcControlId,ids));

	}

	/**
	 * @Description 批量取消网关列表
	 * @Author daina
	 * @Date 10:00 2020/2/22
	 * @param opcControlId
	 * @param ids
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/cancelAuthGatewayAll")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	@ApiLog("批量取消网关列表")
	@Log(title = "批量取消网关列表", businessType = BusinessType.DELETE)
	public R cancelAuthGatewayAll(@ApiParam(name ="opcControlId", value = "控制id") @RequestParam Long  opcControlId,@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		int gateways = gateControlService.deleteGateways(opcControlId, ids);
		if(gateways>0){
			return  R.status(true);
		}else{
			return  R.status(false);
		}
	}

	/**
	 * @Description 查询未分配的机构列表
	 * @Author daina
	 * @Date 10:00 2020/2/22
	 * @param orga
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.orga.vo.OrgaVO>>
	 **/
	@GetMapping("/UnControllerOrgaGateList")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "orga")
	@ApiLog("查询未分配的机构列表")
	@Log(title = "查询未分配的机构列表",businessType = BusinessType.SELECT)
	public R<IPage<OrgaVO>> unControllerOrgaGateList(@ApiIgnore OrgaVO orga, Query query){
		IPage<OrgaVO> pages = orgaService.UnControllerOrgaGateList(Condition.getPage(query), orga);
		return R.data(pages);
	}

	/**
	 * @Description 批量选择机构
	 * @Author daina
	 * @Date 10:00 2020/2/22
	 * @param opcControlId
	 * @param ids
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/AuthControllerOrgaAll")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入orga")
	@ApiLog("批量添加机构信息")
	@Log(title = "批量添加机构信息", businessType = BusinessType.INSERT)
	public R authControllerOrgaAll(@ApiParam(name ="opcControlId", value = "控制id") @RequestParam Long  opcControlId,@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return  R.status(orgaGatewayService.batchControllerOrga(opcControlId,ids));
	}

	/**
	 * @Description 批量取消网关机构列表
	 * @Author daina
	 * @Date 10:00 2020/2/22
	 * @param opcControlId
	 * @param ids
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/cancelAuthControllerOrgaGateAll")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	@ApiLog("批量取消机构信息")
	@Log(title = "批量取消机构信息", businessType = BusinessType.DELETE)
	public R cancelAuthControllerOrgaGateAll(@ApiParam(name ="opcControlId", value = "控制id") @RequestParam Long  opcControlId,@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		int gateways = orgaGatewayService.deleteOrgas(opcControlId, ids);
		if(gateways>0){
			return  R.status(true);
		}else{
			return  R.status(false);
		}
	}

	/**
	 * @Description 查询未分配的渠道列表
	 * @Author daina
	 * @Date 10:00 2020/2/22
	 * @param app
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.orga.vo.AppVO>>
	 **/
	@GetMapping("/UnControllerGateAppList")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入app")
	@ApiLog("查询未分配的渠道列表")
	@Log(title = "查询未分配的渠道列表",businessType = BusinessType.SELECT)
	public R<IPage<AppVO>> unControllerGateAppList(@ApiIgnore AppVO app, Query query){
		IPage<AppVO> pages = appService.UnControllerOrgaGateList(Condition.getPage(query), app);
		return R.data(pages);
	}

	/**
	 * @Description 批量选择渠道
	 * @Author daina
	 * @Date 10:01 2020/2/22
	 * @param opcControlId
	 * @param ids
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/AuthControllerAppAll")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入id")
	@ApiLog("批量添加渠道信息")
	@Log(title = "批量添加渠道信息", businessType = BusinessType.INSERT)
	public R authControllerAppAll(@ApiParam(name ="opcControlId", value = "控制id") @RequestParam Long  opcControlId,@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return  R.status(appGatewayService.batchControllerApp(opcControlId,ids));

	}

	/**
	 * @Description 批量取消渠道列表
	 * @Author daina
	 * @Date 10:01 2020/2/22
	 * @param opcControlId
	 * @param ids
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/cancelAuthControllerAppAll")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	@ApiLog("批量取消渠道")
	@Log(title = "批量取消渠道", businessType = BusinessType.DELETE)
	public R cancelAuthControllerAppAll(@ApiParam(name ="opcControlId", value = "控制id") @RequestParam Long  opcControlId,@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		int gateways = appGatewayService.deleteApps(opcControlId, ids);
		if(gateways>0){
			return  R.status(true);
		}else{
			return  R.status(false);
		}
	}
}
